<?php
class Network_teached extends basicNetwork_teached
{
    public function getResult($inArray)
    {
        $in = array($inArray);
        return $this->getResults($in);
    }
    
    public function afterSave() {
        parent::afterSave();
        $this->saveTrainCompare();
    }
    
    public function saveTrainCompare(){
       $trainFile = Training_data::loadModel($this->id_training_file);
       $array = ChartData::prepareArray($trainFile->file, $this->getResultsFromFile($trainFile->file));
       ChartData::saveData(ChartData::tableNetwork, $this->id_network_teached, Data_network::typeCompare, $array);
    }
    
    public function getResults($inArray)
    {
        
        if($this->NewRecord)
        {
            throw new Exception("Can not run not teached network");
        }
        $structure = Network_prepared::loadModel($this->id_network_prepared, array('Network'))->id_network->structure;
        $outNumber = array_pop(explode('-', $structure));
        
        
        $string = count($inArray).' '.count($inArray[0]).' '.$outNumber."\n";
        foreach($inArray as $inputs)
        {
            foreach($inputs as $input)
            {
                $string .= $input.' ';
                
            }
            
            $string = rtrim($string, ' ');
            $string .= "\n";
           
            for($i =0; $i<$outNumber; $i++)
            {
                $string .= $i.' ';
            }
            $string = rtrim($string, ' ');
            $string .= "\n";
        }
        
        file_put_contents('temp.data', $string);
        return $this->getResultsFromFile('temp.data'); 
          
    }
    
    public function getResultsFromFile($file)
    {
        copy($file, "temp.data");
        
        copy($this->file, basename($this->file));
        $params = BasicFunctions::prepareFileName($this->name).' temp.data';
        
        exec(ConfigPath::network_run.' '.$params, $output);
       
        
        if($output[0] != '1')
        {
            throw new Exception('NETWORK_RUN failed');
        }
        unlink(basename($this->file));
        unlink("temp.data");
        return array_slice($output,1);
    }
    
    public function getErrorFromFile($file)
    {
        copy($file, "temp.data");
        copy($this->file, basename($this->file));
        $params = BasicFunctions::prepareFileName($this->name).' temp.data';
      
        exec(ConfigPath::network_test_global.' '.$params, $output);
        if($output[0] != '1')
        {
            throw new Exception('NETWORK_TEST_GLOBAL failed');
        }
        
        unlink(basename($this->file));
        unlink("temp.data");
        return array_slice($output,1);
    }

}
?>